﻿using IBLl;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model;
using NPOI.XSSF.UserModel;

namespace erwuliu.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class fanghaoController : ControllerBase
    {
        public fanghaoController(ifanghaobll bll)
        {
            Bll = bll;
        }

        public ifanghaobll Bll { get; }

        [HttpGet]
        public IActionResult daochu()
        {
            var rph = Bll.daochu();
            XSSFWorkbook wb = new XSSFWorkbook();
            var dingyi = wb.CreateSheet("房号信息表");
            var rwo0 = dingyi.CreateRow(0);
            rwo0.CreateCell(0).SetCellValue("名称");
            rwo0.CreateCell(1).SetCellValue("电话");
            rwo0.CreateCell(2).SetCellValue("性别");
            rwo0.CreateCell(3).SetCellValue("年龄");
            rwo0.CreateCell(4).SetCellValue("地址");
            rwo0.CreateCell(5).SetCellValue("创建人");
            rwo0.CreateCell(6).SetCellValue("创建时间");
            rwo0.CreateCell(7).SetCellValue("修改人");
            rwo0.CreateCell(8).SetCellValue("修改时间");
            rwo0.CreateCell(9).SetCellValue("删除人");
            rwo0.CreateCell(10).SetCellValue("删除时间");
            rwo0.CreateCell(11).SetCellValue("删除状态");
            int index = 1;
            foreach (var item in rph)
            {
                var row = dingyi.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.name);
                row.CreateCell(1).SetCellValue(item.phone);
                row.CreateCell(2).SetCellValue(item.sex);
                row.CreateCell(3).SetCellValue(item.age);
                row.CreateCell(4).SetCellValue(item.dizhi);
                row.CreateCell(5).SetCellValue(item.createid);
                row.CreateCell(6).SetCellValue(item.createtime.ToString("yyyy-MM-dd HH:mm:ss"));
                row.CreateCell(7).SetCellValue(item.updateid);
                row.CreateCell(8).SetCellValue(item.updatedate==null?"":item.updatedate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                row.CreateCell(9).SetCellValue(item.deleteid);
                row.CreateCell(10).SetCellValue(item.deletrdate==null?"": item.deletrdate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                row.CreateCell(11).SetCellValue(item.isdel);
                index++;
            }
            byte[] bs = null;
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                bs = ms.ToArray();
            }
            var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bs, mime, "房号等级文件.xlsx");
        }
        [HttpGet]
        public dynamic xianshi(int pageindex, int pagesize)
        {
            return Bll.xianshi(pageindex, pagesize);
        }
        [HttpPost]
        public Task<int> daoru(IFormFile file)
        {       
            MemoryStream memory = new MemoryStream();           
            file.CopyTo(memory);
            memory.Position = 0;
            XSSFWorkbook wb = new XSSFWorkbook(memory);
            var sheet = wb.GetSheet("房号信息表");
            int num = sheet.LastRowNum;
            List<fanghaoModel> list = new List<fanghaoModel>();
            for (int i = 1; i <= num; i++)
                {
                    var row = sheet.GetRow(i);
                    fanghaoModel fanghao = new fanghaoModel();
                    fanghao.name = row.GetCell(0).ToString();
                    fanghao.phone = row.GetCell(1).ToString();
                    fanghao.sex = row.GetCell(2).ToString();
                    fanghao.age = Convert.ToInt32(row.GetCell(3).ToString());
                    fanghao.dizhi = row.GetCell(4).ToString();
                    fanghao.createid = Convert.ToInt32(row.GetCell(5).ToString());
                    fanghao.createtime = Convert.ToDateTime(row.GetCell(6).ToString());
                    fanghao.updateid = Convert.ToInt32(row.GetCell(7).ToString());
                    fanghao.updatedate = row.GetCell(8).ToString() == "" ? null : Convert.ToDateTime(row.GetCell(8).ToString());
                    fanghao.deleteid = Convert.ToInt32(row.GetCell(9).ToString());
                    fanghao.deletrdate = row.GetCell(10).ToString() == "" ? null : Convert.ToDateTime(row.GetCell(10).ToString());
                    fanghao.isdel = row.GetCell(11).BooleanCellValue;


                    list.Add(fanghao);
                }
            return Bll.AddRnagefanghoa(list);           
        }
        [HttpPost]
        public async Task<int> AddRnagefanghoa(List<fanghaoModel> fanghao)
        {
            return await Bll.AddRnagefanghoa(fanghao);
        }


        
        
    }
}
